var express = require('express');
var router = express.Router();
// PizZip is required because docx/pptx/xlsx files are all zipped files, and
// the PizZip library allows us to load the file in memory
const fs = require("fs");
const path = require('path')
const log4js = require("../utils/log")
const getWordLogger = log4js.getLogger('get_word')

router.get('/stay_winter/:file', async function (req, res, next) {
    try {
        const { file } = req.params
        const filePath = path.resolve(__dirname, `../public/files/stay_winter_files/${file}`)
        await new Promise((resolve, reject) => {
            res.download(filePath, function (err) {
                if (err&&err.message!='Request aborted') {
                    //如果用户取消下载了 就报request aborted请求中断 如果是request aborted就别报错了 不是很重要
                    reject(err)
                } else {
                    resolve('success')
                }
            })
        })
        await new Promise((resolve, reject) => {
            fs.unlink(filePath, function (err) {
                if (err) {
                    reject(err)
                } else {
                    resolve('success')
                }
            })
        })
    } catch (err) {
        console.log(err)
        getWordLogger.error(err)
    }
});

module.exports = router;
